home *** CD-ROM | disk | FTP | other *** search
- /*
- * awk.h -- Definitions for gawk/hawk.
- */
-
- /* Copyright © 1986, 1988, 1989 1991 the Free Software Foundation, Inc.
- * This file is part of GAWK, the GNU implementation of the
- * AWK Progamming Language, modified for the Macintosh (also called hAWK).
- * GAWK is free software; you can redistribute or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 1, or any later version.
- * GAWK is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with GAWK; see the file "COPYING hAWK". If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- * Modified for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
- */
- /* Mac note - in this file most of the declarations dealing with
- portability have been left in place. However, chunks of code not for the
- Mac version HAVE been chopped out in the overall project, and so if it is
- your ambition to produce a version of AWK for some other computer you should
- obtain the original ibm/unix version of this code.
- */
- #ifndef MACVERSION
- #define MACVERSION 1
- #endif
-
- /* ------------------------------ Includes ------------------------------ */
- #include <stdio.h>
- #include <ctype.h>
- #include <setjmp.h>
- #include <_stdarg.h>
- #include <errno.h>
- #ifndef MACVERSION
- #include <sys/types.h>
- #include <sys/stat.h>
- #else
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <unix.h>
- #include <time.h>
- #endif
-
-
- #include "REGEX.H"
-
- /* ------------------- System Functions, Variables, etc ------------------- */
- #ifndef MACVERSION
- /* nasty nasty SunOS-ism */
- #ifdef sparc
- #include <alloca.h>
- #ifdef lint
- extern char *alloca();
- #endif
- #else
- extern char *alloca();
- #endif
- #ifdef SPRINTF_INT
- extern short sprintf();
- #else /* not USG */
- /* nasty nasty berkelixm */
- #define setjmp _setjmp
- #define longjmp _longjmp
-
- extern char *sprintf();
- #endif
- #endif
-
- #ifdef MACVERSION
- extern void *alloca(unsigned short size);
- #endif
-
- #ifndef MACVERSION
- /*
- * if you don't have vprintf, but you are BSD, the version defined in
- * vprintf.c should do the trick. Otherwise, use this and cross your fingers.
- SEE iomisc.c, jes i got it.
- */
- #if defined(VPRINTF_MISSING) && !defined(DOPRNT_MISSING) && !defined(BSDSTDIO)
- #define vfprintf(fp,fmt,arg) _doprnt((fmt), (arg), (fp))
- #endif
- #endif
-
- #ifdef MACVERSION
- extern short strcasecmp(char *s1, char *s2);
- extern short strncasecmp(char *s1, char *s2, register short n);
- #else
- #ifdef __STDC__
- extern void *malloc(unsigned), *realloc(void *, unsigned);
- extern void free(char *);
- extern char *getenv(char *);
-
- extern char *strcpy(char *, char *), *strcat(char *, char *), *strncpy(char *, char *, short);
- extern short strcmp(char *, char *);
- extern short strncmp(char *, char *, short);
- extern short strncasecmp(char *, char *, short);
- extern char *strerror(short);
- extern char *strchr(char *, short);
- extern short strlen(char *);
- extern char *memcpy(char *, char *, short);
- extern short memcmp(char *, char *, short);
- extern char *memset(char *, short, short);
-
- /* extern short fprintf(FILE *, char *, ...); */
- extern short fprintf();
- extern short vfprintf();
- #ifndef MSDOS
- extern short fwrite(char *, short, short, FILE *);
- #endif
- extern short fflush(FILE *);
- extern short fclose(FILE *);
- extern short pclose(FILE *);
- #ifndef MSDOS
- extern short fputs(char *, FILE *);
- #endif
- extern void abort();
- extern short isatty(short);
- extern void exit(short);
- extern short system(char *);
- extern short sscanf(/* char *, char *, ... */);
-
- extern double atof(char *);
- extern short fstat(short, struct stat *);
- extern off_t lseek(short, off_t, short);
- extern short fseek(FILE *, long, short);
- extern short close(short);
- extern short open();
- extern short pipe(short *);
- extern short dup2(short, short);
- #ifndef MSDOS
- extern short unlink(char *);
- #endif
- extern short fork();
- extern short execl(/* char *, char *, ... */);
- extern short read(short, char *, short);
- extern short wait(short *);
- extern void _exit(short);
- #else
- extern void _exit();
- extern short wait();
- extern short read();
- extern short execl();
- extern short fork();
- extern short unlink();
- extern short dup2();
- extern short pipe();
- extern short open();
- extern short close();
- extern short fseek();
- extern off_t lseek();
- extern short fstat();
- extern void exit();
- extern short system();
- extern short isatty();
- extern void abort();
- extern short fputs();
- extern short fclose();
- extern short pclose();
- extern short fflush();
- extern short fwrite();
- extern short fprintf();
- extern short vfprintf();
- extern short sscanf();
- extern char *malloc(), *realloc();
- extern void free();
- extern char *getenv();
-
- extern short strcmp();
- extern short strncmp();
- extern short strncasecmp();
- extern short strlen();
- extern char *strcpy(), *strcat(), *strncpy();
- extern char *memset();
- extern short memcmp();
- extern char *memcpy();
- extern char *strerror();
- extern char *strchr();
-
- extern double atof();
- #endif
- #endif
-
- extern int errno;
-
- /* ------------------ Constants, Structures, Typedefs ------------------ */
- #define AWKNUM double
-
- typedef enum {
- /* illegal entry == 0 */
- Node_illegal,
-
- /* binary operators lnode and rnode are the expressions to work on */
- Node_times,
- Node_quotient,
- Node_mod,
- Node_plus,
- Node_minus,
- Node_cond_pair, /* conditional pair (see Node_line_range) */
- Node_subscript,
- Node_concat,
- Node_exp,
-
- /* unary operators subnode is the expression to work on */
- /*10*/ Node_preincrement,
- Node_predecrement,
- Node_postincrement,
- Node_postdecrement,
- Node_unary_minus,
- Node_field_spec,
-
- /* assignments lnode is the var to assign to, rnode is the exp */
- Node_assign,
- Node_assign_times,
- Node_assign_quotient,
- Node_assign_mod,
- /*20*/ Node_assign_plus,
- Node_assign_minus,
- Node_assign_exp,
-
- /* boolean binaries lnode and rnode are expressions */
- Node_and,
- Node_or,
-
- /* binary relationals compares lnode and rnode */
- Node_equal,
- Node_notequal,
- Node_less,
- Node_greater,
- Node_leq,
- /*30*/ Node_geq,
- Node_match,
- Node_nomatch,
-
- /* unary relationals works on subnode */
- Node_not,
-
- /* program structures */
- Node_rule_list, /* lnode is a rule, rnode is rest of list */
- Node_rule_node, /* lnode is pattern, rnode is statement */
- Node_statement_list, /* lnode is statement, rnode is more list */
- Node_if_branches, /* lnode is to run on true, rnode on false */
- Node_expression_list, /* lnode is an exp, rnode is more list */
- Node_param_list, /* lnode is a variable, rnode is more list */
-
- /* keywords */
- /*40*/ Node_K_if, /* lnode is conditonal, rnode is if_branches */
- Node_K_while, /* lnode is condtional, rnode is stuff to run */
- Node_K_for, /* lnode is for_struct, rnode is stuff to run */
- Node_K_arrayfor, /* lnode is for_struct, rnode is stuff to run */
- Node_K_break, /* no subs */
- Node_K_continue, /* no stuff */
- Node_K_print, /* lnode is exp_list, rnode is redirect */
- Node_K_printf, /* lnode is exp_list, rnode is redirect */
- Node_K_next, /* no subs */
- Node_K_exit, /* subnode is return value, or NULL */
- Node_K_do, /* lnode is conditional, rnode stuff to run */
- Node_K_return,
- Node_K_delete,
- Node_K_getline,
- Node_K_function, /* lnode is statement list, rnode is params */
-
- /* I/O redirection for print statements */
- Node_redirect_output, /* subnode is where to redirect */
- Node_redirect_append, /* subnode is where to redirect */
- Node_redirect_pipe, /* subnode is where to redirect */
- Node_redirect_pipein, /* subnode is where to redirect */
- Node_redirect_input, /* subnode is where to redirect */
-
- /* Variables */
- Node_var, /* rnode is value, lnode is array stuff */
- Node_var_array, /* array is ptr to elements, asize num of
- * eles */
- Node_val, /* node is a value - type in flags */
-
- /* Builtins subnode is explist to work on, proc is func to call */
- Node_builtin,
-
- /*
- * pattern: conditional ',' conditional ; lnode of Node_line_range
- * is the two conditionals (Node_cond_pair), other word (rnode place)
- * is a flag indicating whether or not this range has been entered.
- */
- Node_line_range,
-
- /*
- * boolean test of membership in array lnode is string-valued
- * expression rnode is array name
- */
- Node_in_array,
-
- Node_func, /* lnode is param. list, rnode is body */
- Node_func_call, /* lnode is name, rnode is argument list */
-
- Node_cond_exp, /* lnode is conditonal, rnode is if_branches */
- Node_regex,
- Node_hashnode,
- Node_ahash
- } NODETYPE;
-
- /*
- * NOTE - this struct is a rather kludgey -- it is packed to minimize
- * space usage, at the expense of cleanliness. Alter at own risk.
- */
- typedef struct exp_node {
- union {
- struct {
- union {
- struct exp_node *lptr;
- char *param_name;
- char *retext;
- struct exp_node *nextnode;
- } l;
- union {
- struct exp_node *rptr;
- struct exp_node *(*pptr) (struct exp_node *);
- struct re_pattern_buffer *preg;
- struct for_loop_header *hd;
- struct exp_node **av;
- short r_ent; /* range entered */
- } r;
- char *name;
- short number;
- unsigned char recase;
- } nodep;
- struct {
- AWKNUM fltnum; /* this is here for optimal packing of
- * the structure on many machines
- */
- char *sp;
- short slen;
- unsigned char sref;
- } val;
- struct {
- struct exp_node *next;
- char *name;
- short length;
- struct exp_node *value;
- } hash;
- #define hnext sub.hash.next
- #define hname sub.hash.name
- #define hlength sub.hash.length
- #define hvalue sub.hash.value
- struct {
- struct exp_node *next;
- struct exp_node *name;
- struct exp_node *value;
- } ahash;
- #define ahnext sub.ahash.next
- #define ahname sub.ahash.name
- #define ahvalue sub.ahash.value
- } sub;
- NODETYPE type;
- unsigned char flags;
- # define MEM 0x7
- # define MALLOC 1 /* can be free'd */
- # define TEMP 2 /* should be free'd */
- # define PERM 4 /* can't be free'd */
- # define VAL 0x18
- # define NUM 8 /* numeric value is valid */
- # define STR 16 /* string value is valid */
- # define NUMERIC 32 /* entire field is numeric */
- } NODE;
-
- #define lnode sub.nodep.l.lptr
- #define nextp sub.nodep.l.nextnode
- #define rnode sub.nodep.r.rptr
- #define source_file sub.nodep.name
- #define source_line sub.nodep.number
- #define param_cnt sub.nodep.number
- #define param sub.nodep.l.param_name
-
- #define subnode lnode
- #define proc sub.nodep.r.pptr
-
- #define reexp lnode
- #define rereg sub.nodep.r.preg
- #define re_case sub.nodep.recase
- #define re_text sub.nodep.l.retext
-
- #define forsub lnode
- #define forloop rnode->sub.nodep.r.hd
-
- #define stptr sub.val.sp
- #define stlen sub.val.slen
- #define stref sub.val.sref
- #define valstat flags
-
- #define numbr sub.val.fltnum
-
- #define var_value lnode
- #define var_array sub.nodep.r.av
-
- #define condpair lnode
- #define triggered sub.nodep.r.r_ent
-
- #define HASHSIZE 101
-
- typedef struct for_loop_header {
- NODE *init;
- NODE *cond;
- NODE *incr;
- } FOR_LOOP_HEADER;
-
- /* for "for(iggy in foo) {" */
- struct search {
- short numleft;
- NODE **arr_ptr;
- NODE *bucket;
- NODE *retval;
- };
-
- /* for faster input, bypass stdio */
- typedef struct iobuf {
- FILE * fd;
- char *buf;
- char *off;
- short size; /* this will be determined by an fstat() call */
- short cnt;
- char *secbuf;
- short secsiz;
- short flag;
- # define IOP_IS_TTY 1
- } IOBUF;
-
- /*
- * structure used to dynamically maintain a linked-list of open files/pipes
- */
- struct redirect {
- short flag;
- # define RED_FILE 1
- # define RED_PIPE 2
- # define RED_READ 4
- # define RED_WRITE 8
- # define RED_APPEND 16
- # define RED_NOBUF 32
- char *value;
- FILE *fp;
- IOBUF *iop;
- short pid;
- short status;
- long offset; /* used for dynamic management of open files */
- struct redirect *prev;
- struct redirect *next;
- };
-
- /* longjmp return codes, must be nonzero */
- /* Continue means either for loop/while continue, or next input record */
- #define TAG_CONTINUE 1
- /* Break means either for/while break, or stop reading input */
- #define TAG_BREAK 2
- /* Return means return from a function call; leave value in ret_node */
- #define TAG_RETURN 3
-
- /*
- #ifdef MSDOS
- #define HUGE 0x7fff
- #else
- #define HUGE 0x7fffffff
- #endif
- */
- #ifdef MACVERSION
- #define HUGE 0x7ffe
- #endif
-
- /* -------------------------- External variables -------------------------- */
- /* gawk builtin variables */
- extern NODE *FS_node, *NF_node, *RS_node, *NR_node;
- extern NODE *FILENAME_node, *OFS_node, *ORS_node, *OFMT_node;
- extern NODE *FNR_node, *RLENGTH_node, *RSTART_node, *SUBSEP_node;
- extern NODE *IGNORECASE_node;
-
- extern NODE **stack_ptr;
- extern NODE *Nnull_string;
- extern NODE *deref;
- extern NODE **fields_arr;
- extern short sourceline;
- extern char *source;
- extern NODE *expression_value;
-
- extern NODE *variables[];
-
- extern NODE *_t; /* used as temporary in tree_eval */
-
- extern char *myname;
-
- extern short node0_valid;
- extern short field_num;
- extern short strict;
-
-
- /* ------------- Function prototypes or defs (as appropriate) ------------- */
- /*#ifdef __STDC__*/
- #ifdef MACVERSION
- extern short parse_escape(char **);
- extern FILE * devopen(char *, char *);
- extern struct re_pattern_buffer *make_regexp(NODE *, short);
- extern struct re_pattern_buffer *mk_re_parse(char *, short);
- extern NODE *variable(char *);
- extern NODE *install(NODE **, char *, NODE *);
- extern NODE *lookup(NODE **, char *);
- extern NODE *make_name(char *, NODETYPE);
- extern short interpret(NODE *);
- extern NODE *r_tree_eval(NODE *);
- extern void assign_number(NODE **, double);
- extern short cmp_nodes(NODE *, NODE *);
- extern struct redirect *redirect(NODE *, short *);
- extern short flush_io(void);
- extern void print_simple(NODE *, FILE *);
- /* extern void warning(char *,...); */
- extern void warning(char *va_alist, ...);
- /* extern void fatal(char *,...); */
- extern void fatal(char *va_alist, ...);
- extern void set_record(char *, short);
- extern NODE **get_field(short, short);
- extern NODE **get_lhs(NODE *, short);
- extern void do_deref(void );
- extern struct search *assoc_scan(NODE *);
- extern struct search *assoc_next(struct search *);
- extern NODE **assoc_lookup(NODE *, NODE *);
- extern double r_force_number(NODE *);
- extern NODE *r_force_string(NODE *);
- extern NODE *newnode(NODETYPE);
- extern NODE *dupnode(NODE *);
- extern NODE *make_number(double);
- extern NODE *tmp_number(double);
- extern NODE *make_str_node(char *, short, short);
- extern NODE *tmp_string(char *, short);
- extern char *re_compile_pattern(char *, short, struct re_pattern_buffer *);
- extern short re_search(struct re_pattern_buffer *, char *, short, short, short, struct re_registers *);
- extern void freenode(NODE *);
- #endif
-
-
- /* ------------------------- Pseudo-functions ------------------------- */
- #ifdef MACVERSION
- #include "CodeResHelper.h" /* TMalloc etc */
- extern void JumpOnHAWKError(short inputErrorNumber);
-
- /*********** early version, worked but was inefficient
- #define malloc(x) TMalloc(x)
- #define realloc(x, y) Trealloc(x, y)
- #define free(x) Tfree(x)
- ******************************************************/
-
- #define malloc(x) Fmalloc(x)
- #define realloc(x, y) Frealloc(x, y)
- #define free(x) Ffree(x)
-
- #define abort() JumpOnHAWKError(4)
- #define exit(x) JumpOnHAWKError(x)
- #define popen(str, w) NULL
- #define pclose(x) 1
- #define CheckForInterrupt() if (TaskWasInterrupted()) JumpOnHAWKError(999)
-
- extern jmp_buf envBuf;
-
- #endif MACVERSION
-
- #define is_identchar(c) (isalnum(c) || (c) == '_')
-
-
- #define free_temp(n) if ((n)->flags&TEMP) { deref = (n); do_deref(); } else
- /* Note 'else' is smothered by the usual following semicolon - purpose unknown. */
- #define tree_eval(t) (_t = (t),(_t) == NULL ? Nnull_string : \
- ((_t)->type == Node_val ? (_t) : r_tree_eval((_t))))
- #define make_string(s,l) make_str_node((s),(l),0)
-
- #define cant_happen() fatal("line %d, file: %s; bailing out", \
- __LINE__, __FILE__);
- #ifdef MEMDEBUG
- #define memmsg(x,y,z,zz) fprintf(stderr, "malloc: %s: %s: %d %0x\n", z, x, y, zz)
- #define free(s) fprintf(stderr, "free: s: %0x\n", s), do_free(s)
- #else
- #define memmsg(x,y,z,zz)
- #endif
-
- #define emalloc(var,ty,x,str) if ((var = (ty) malloc((unsigned)(x))) == NULL)\
- fatal("%s: %s: can't allocate memory (%s)",\
- (str), "var", strerror(errno)); else\
- memmsg("var", x, str, var)
- #define erealloc(var,ty,x,str) if((var=(ty)realloc((char *)var,\
- (unsigned)(x)))==NULL)\
- fatal("%s: %s: can't allocate memory (%s)",\
- (str), "var", strerror(errno)); else\
- memmsg("re: var", x, str, var)
- #ifdef DEBUG
- #define force_number r_force_number
- #define force_string r_force_string
- #else
- #ifdef lint
- extern AWKNUM force_number();
- #endif
- #ifdef MSDOS
- extern double _msc51bug;
- #define force_number(n) (_msc51bug=(_t = (n),(_t->flags & NUM) ? _t->numbr : r_force_number(_t)))
- #else
- #define force_number(n) (_t = (n),(_t->flags & NUM) ? _t->numbr : r_force_number(_t))
- #endif
- #define force_string(s) (_t = (s),(_t->flags & STR) ? _t : r_force_string(_t))
- #endif
-
- #define STREQ(a,b) (*(a) == *(b) && strcmp((a), (b)) == 0)
- #define STREQN(a,b,n) ((n) && *(a) == *(b) && strncmp((a), (b), (n)) == 0)
-
- #define WHOLELINE (node0_valid ? fields_arr[0] : *get_field(0,0))
-
-
- #ifdef MACVERSION
- #if !defined(__STDC__) || __STDC__ <= 0
- #define volatile
- #endif
- #endif
-
- /* Figure out what '\a' really is. */
- #ifdef __STDC__
- #define BELL '\a' /* sure makes life easy, don't it? */
- #else
- # if 'z' - 'a' == 25 /* ascii */
- # if 'a' != 97 /* machine is dumb enough to use mark parity */
- # define BELL '\207'
- # else
- # define BELL '\07'
- # endif
- # else
- # define BELL '\057'
- # endif
- #endif
-
- #ifndef SIGTYPE
- #define SIGTYPE void
- #endif
-
- extern char casetable[]; /* for case-independent regexp matching */
-
-
- #ifdef MACVERSION
- #include "hAWK_recurse.h"
- #endif
-
-